package itstudio.instructor.fragment;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import itstudio.instructor.config.Config;
import itstudio.instructor.config.MyApplication;
import itstudio.instructor.config.ImageOptionsUtil;
import itstudio.instructor.entity.User;
import itstudio.instructor.ui.LoginActivity;
import itstudio.instructor.ui.MainActivity;
import itstudio.instructor.widget.CustomScrollView;
import itstudio.app.wq.R;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @Description 侧滑左Fragment
 * 
 * @author MR.Wang
 * 
 * @date 2014-7-5 上午1:13:57
 * 
 * @version V1.0
 */

public class LeftDrawerFragment extends Fragment {

	// toolboxLayout
	private RelativeLayout toolboxHome;
	private View toolboxIntructor;
	private View toolboxPublic;
	private View toolboxCollect;
	private View toolboxSetting;
	public static  View toolboxSend;
	public static  View toolboxDrafts;
	private View toolboxMessage;
	private ImageView new_message_iv;

	private View currentView; // 当前选中的View
	private View rootView; // 当前整个View
	private static ImageView  headImageView; // 左侧图像 ImageView
	public static TextView nameTv; // 左侧名字 textView
	private TextView titleTv; // 顶部栏目标题 textView
	Fragment newContent = null;
	private CustomScrollView scrollView = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (MyApplication.user!=null) {
			if (MyApplication.user.getNewReply() > 0) {
				new_message_iv.setVisibility(View.VISIBLE);
			}else {
				new_message_iv.setVisibility(View.GONE);
			}
			
		}
	}
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.main_left_fragment, container,
				false);
		findViewById();
		setOnclick();
		if (MyApplication.user!=null) {
			if (MyApplication.user.getNewReply() > 0) {
				new_message_iv.setVisibility(View.VISIBLE);
			}
		}
		return rootView;
	}

	protected void findViewById() {

		// 设置CustomScrollView
		ImageView backgroundImageView = (ImageView) rootView
				.findViewById(R.id.personal_background_image);
		scrollView = (CustomScrollView) rootView
				.findViewById(R.id.personal_scrollView);
		scrollView.setImageView(backgroundImageView);

		// findbyID
		toolboxHome = (RelativeLayout) rootView
				.findViewById(R.id.toolbox_layout_home);
		toolboxIntructor = rootView
				.findViewById(R.id.toolbox_layout_instructor);
		toolboxPublic = rootView.findViewById(R.id.toolbox_layout_community);
		toolboxCollect = rootView.findViewById(R.id.toolbox_layout_collect);
		toolboxSetting = rootView.findViewById(R.id.toolbox_layout_setting);
		toolboxSend = rootView.findViewById(R.id.toolbox_layout_send);
		toolboxDrafts = rootView.findViewById(R.id.toolbox_layout_drafts);
		toolboxMessage = rootView.findViewById(R.id.toolbox_layout_message);
		new_message_iv=(ImageView) rootView.findViewById(R.id.new_message);

		headImageView = (ImageView) rootView.findViewById(R.id.headImageView);
		nameTv = (TextView) rootView.findViewById(R.id.nameTextView);

		titleTv = MainActivity.titleTv;
		currentView = toolboxHome;
		currentView.setSelected(true);

	}

	public static void setInfo(){
		
	if(LeftDrawerFragment.nameTv !=null){
		if(MyApplication.user!=null){
			LeftDrawerFragment.nameTv.setText(MyApplication.user.getName());
			if(MyApplication.user.getStatus()==0){
				LeftDrawerFragment.toolboxSend.setVisibility(View.GONE);
				LeftDrawerFragment.toolboxDrafts.setVisibility(View.GONE);
			}
			DisplayImageOptions options = ImageOptionsUtil.headImageOptions();
			ImageLoader imageLoader = ImageLoader.getInstance();
			imageLoader.displayImage(Config.HEAD_URL + MyApplication.user.getHeadUrl(),
					headImageView, options);
		}
		else{
			nameTv.setText(R.string.login_hint);
			headImageView.setImageResource(R.drawable.head_default_local);
			toolboxSend.setVisibility(View.VISIBLE);
			toolboxDrafts.setVisibility(View.VISIBLE);
		}
	}

	}
/*	@Override
	public void onResume() {
		super.onResume();	
		setInfo();
	}*/
	
	protected void setOnclick() {

		// 添加监听事件
		MyOClick click = new MyOClick();
		toolboxHome.setOnClickListener(click);
		toolboxIntructor.setOnClickListener(click);
		toolboxPublic.setOnClickListener(click);
		toolboxCollect.setOnClickListener(click);
		toolboxSetting.setOnClickListener(click);
		toolboxSend.setOnClickListener(click);
		toolboxDrafts.setOnClickListener(click);
		toolboxMessage.setOnClickListener(click);
		headImageView.setOnClickListener(click);

	}

	class MyOClick implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			Intent intent = new Intent();
			switch (v.getId()) {

			case R.id.toolbox_layout_home:
				//newContent = FragmentHome.getInstance(getActivity());
				newContent = new FragmentHome();
				titleTv.setText(R.string.home);
				currentView.setSelected(false);
				currentView = toolboxHome;
				currentView.setSelected(true);
				break;

			case R.id.toolbox_layout_instructor:
				if(currentView != toolboxIntructor){
					newContent = new FragmentTeacher();
				}
				titleTv.setText(R.string.instructor);
				currentView.setSelected(false);
				currentView = toolboxIntructor;
				currentView.setSelected(true);
				break;

			case R.id.toolbox_layout_community:
				
				if(currentView != toolboxPublic){
					newContent = new FragmentPub();
				}
				titleTv.setText(R.string.public_num);
				currentView.setSelected(false);
				currentView = toolboxPublic;
				currentView.setSelected(true);
				break;

			case R.id.toolbox_layout_send:
				
				if (MyApplication.user==null) {
					delayLoginActivity();
				}else if(MyApplication.user.getStatus()!=User.STATUS_STUDENT){
					newContent = FragmentSend.getInstance(getActivity());
					titleTv.setText(R.string.edit);
					currentView.setSelected(false);
					currentView = toolboxSend;
					currentView.setSelected(true);
				}
				break;

			case R.id.toolbox_layout_drafts:
				if (MyApplication.user==null) {
					delayLoginActivity();
				}
				else{
					newContent = FragmentDrafts.getInstance(getActivity());
				}
				
				titleTv.setText(R.string.drafts);
				currentView.setSelected(false);
				currentView = toolboxDrafts;
				currentView.setSelected(true);
				break;

			case R.id.toolbox_layout_message:
				if (MyApplication.user==null) {
					delayLoginActivity();
				}
				else if(currentView != toolboxMessage){
					newContent = new FragmentMessage();
				}

				titleTv.setText(R.string.message);
				new_message_iv.setVisibility(View.GONE);
				currentView.setSelected(false);
				currentView = toolboxMessage;
				currentView.setSelected(true);
				break;
			case R.id.toolbox_layout_collect:
				
				if (MyApplication.user==null) {
					delayLoginActivity();
				} else {
					newContent = new FragmentCollect();
					titleTv.setText(R.string.collect);
					currentView.setSelected(false);
					currentView = toolboxCollect;
					currentView.setSelected(true);
				}
				break;
			case R.id.toolbox_layout_setting:
				//newContent = FragmentSetting.getInstance(getActivity());
				newContent = new FragmentSetting();
				titleTv.setText(R.string.setting);
				currentView.setSelected(false);
				currentView = toolboxSetting;
				currentView.setSelected(true);
				break;
			case R.id.headImageView:
				if (MyApplication.user==null) {
					
					delayLoginActivity();

				} else {
					//userActivity();
					if(currentView == headImageView){
						
						titleTv.setText(MyApplication.user.getName());
						currentView.setSelected(false);
						currentView = headImageView;
						currentView.setSelected(true);
					}else{
						delayFragmentSelf();
						break;
					}
					
				}
				break;

			default:
				break;
			}
			if (newContent != null)
				switchFragment(newContent);
		}

	}
	//延迟跳转登陆
	private void delayLoginActivity(){
		((MainActivity) getActivity()).closeDrawer();
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(getActivity(), LoginActivity.class);
				startActivity(intent);
			}
		},400);
				
	}
	//延迟加载  个人页面
	private void delayFragmentSelf(){
		((MainActivity) getActivity()).closeDrawer();
		
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				newContent = new FragmentSelf();
				switchFragment(newContent);
				titleTv.setText(MyApplication.user.getName());
				currentView.setSelected(false);
				currentView = headImageView;
				currentView.setSelected(true);
			}
		}, 400);
		
	}
		
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null){
			return;
		}
		MainActivity ra = (MainActivity) getActivity();
		ra.switchFrame(fragment);
	}

}
